package com.nsu.evaluation.service.impl;

import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.dao.RoleMapper;
import com.nsu.evaluation.pojo.Role;
import com.nsu.evaluation.pojo.RoleExample;
import com.nsu.evaluation.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("roleService")
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 新增角色信息
     * @param role
     * @return
     */
    @Override
    public ServerResponse<Role> addRole(Role role){
        int rowCount = roleMapper.insert(role);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("新增角色成功",role);
        }
        return ServerResponse.createByErrorMessage("新增角色失败");
    }

    /**
     * 删除角色信息
     * @param roleId
     * @return
     */
    @Override
    public ServerResponse deleteRoleByPrimaryKey(String roleId){
        int rowCount = roleMapper.deleteByPrimaryKey(roleId);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("删除角色成功",roleId);
        }
        return ServerResponse.createByErrorMessage("删除角色失败");
    }

    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @Override
    public ServerResponse<Role> updateRoleByPrimaryKey(Role role){
        int rowCount = roleMapper.updateByPrimaryKey(role);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("修改角色成功",role);
        }
        return ServerResponse.createByErrorMessage("修改角色失败");
    }

    /**
     * 根据角色名或角色Id查询角色信息
     * @param role
     * @return
     */
    @Override
    public ServerResponse<List<Role>> selectRoleByExample(Role role){
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if(null != role.getRoleName() && !role.getRoleName().equals("")){
            criteria.andRoleNameEqualTo(role.getRoleName());
        }
        if(null != role.getId() && !role.getId().equals("")){
            criteria.andIdEqualTo(role.getId());
        }
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        return ServerResponse.createBySuccess("查询角色成功",roleList);
    }

    /**
     * 获取角色列表
     * @return
     */
    @Override
    public ServerResponse<List<Role>> getRoleList(){
        //获取所有角色并返回列表
        RoleExample roleExample = new RoleExample();
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        return ServerResponse.createBySuccess("获取角色列表成功",roleList);
    }
}
